package com.newly.common.core.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.newly.common.base.annotation.NewlyAuth;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.core.utils.RequestUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: newly-parent
 * @remark: 鉴权切面
 * @author: guan
 * @create: 2022-01-08 17:18
 */
@Aspect
@Component
public class NewlyAuthAspect {

    @Autowired
    private RequestUtils requestUtils;


    @Pointcut("@annotation(com.newly.common.base.annotation.NewlyAuth)")
    public void pointCut() {
    }


    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        NewlyAuth newlyAuth = method.getAnnotation(NewlyAuth.class);
        if (ObjectUtil.isEmpty(newlyAuth)) return;


        String[] incRoles = newlyAuth.incRole();
        for (String incRole : incRoles) {
            Object roles = AnnotationResolver.newInstance().resolver(joinPoint, incRole);
        }

        String[] incPerms = newlyAuth.incPerm();
        for (String incPerm : incPerms) {
            System.out.println(incPerm);
            Object perms = AnnotationResolver.newInstance().resolver(joinPoint, incPerm);
            System.out.println(perms);
        }


        // 开发者检测
        //this.checkDev(newlyAuth);

        // 角色检查
        //this.checkRole(joinPoint, newlyAuth);

        /*String[] sourceRoleStrs = newlyAuth.incRole();

        if (!ObjectUtil.isEmpty(sourceRoleStrs)) {
            for (String sourceRoleStr : sourceRoleStrs) {
                Object roles = AnnotationResolver.newInstance().resolver(joinPoint, sourceRoleStr);
                System.out.println(roles);
            }
        }
        String[] sourcePermStrs = newlyAuth.incPerm();
        if (!ObjectUtil.isEmpty(sourceRoleStrs)) {
            for (String sourcePermStr : sourcePermStrs) {
                Object perms = AnnotationResolver.newInstance().resolver(joinPoint, sourcePermStr);
                System.out.println(perms);
            }
        }*/
    }

    private void checkRole(JoinPoint joinPoint, NewlyAuth newlyAuth) {
        String[] roleStrs = newlyAuth.incRole();
        if (!ObjectUtil.isEmpty(roleStrs)) {
            List<Integer> roleIds = new ArrayList<>();
            ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
            for (String roleStr : roleStrs) {
                Object roles = AnnotationResolver.newInstance().resolver(joinPoint, roleStr);
                System.out.println(roles);
            }
            System.out.println(currentTenant.getUserId());
//            for (String roleStr : roleStrs) {
//                Object roles = AnnotationResolver.newInstance().resolver(joinPoint, roleStr);
//                if (roles instanceof List) {
//                    roleIds.addAll((Collection<? extends Integer>) roles);
//                } else if (roles instanceof Integer) {
//                    roleIds.add((Integer) roles);
//                }
//            }
//
//            currentTenant.assertIncludeRoles(roleIds);
        }
    }

    private void checkDev(NewlyAuth newlyAuth) {
        if (newlyAuth.dev()) {
            ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
            currentTenant.assertIsDev();
        }
    }

}
